package com.example.design.taskchain;

import com.example.design.taskchain.concrete.AgeApprover;
import com.example.design.taskchain.concrete.NameApprover;
import com.example.design.taskchain.concrete.RankApprover;

import java.util.ArrayList;
import java.util.List;

/**
 * 责任链模式客户端， 依次处理年龄、姓名、rank的异常数据
 */
public class ApproverClient {
    public static void main(String[] args) {
        //处理年龄
        ApproverHandler doAge = new AgeApprover();
        //处理姓名
        ApproverHandler doName = new NameApprover();
        //处理排名
        ApproverHandler doRank = new RankApprover();

        List<Student> students = getStudent();
        //设置责任链的顺序 age > name > rank
        doAge.setApprover(doName);
        doName.setApprover(doRank);
        doAge.processHandle(students);
        System.out.println(students);

        //这是责任链第一种写法，只要设置好顺序，即可在抽象类种循环调用，要跳出调用链需要单独写钩子程序

        //第二种写法是将调用方法写在具体的实现方法中，这种写法看起来比较冗余，不过灵活些
    }

    public static List<Student> getStudent(){
        List<Student> list = new ArrayList<>();
        list.add(new Student(1,"1", "1", 1));
        list.add(new Student(2,"2", "", 2));
        list.add(new Student(null,"3", "3", 3));
        list.add(new Student(4,"", "4", null));
        list.add(new Student(5,"5", "5", 5));
        return list;
    }
}
